jQuery( document ).ready(function() {    
    
    jQuery.blockUI.defaults.message = "Cargando...";
    jQuery.blockUI.defaults.css.border = 'none'; 
    jQuery.blockUI.defaults.css.padding = '15px'; 
    jQuery.blockUI.defaults.css.backgroundColor = '#000'; 
    jQuery.blockUI.defaults.css.opacity = '.5'; 
    jQuery.blockUI.defaults.css.color = '#fff';      
    jQuery(document).ajaxStart(jQuery.blockUI).ajaxStop(jQuery.unblockUI);    
     
    var split = location.search.replace('?', '').split('=')
    if(split[0] == "post"){
        ajaxCall(split[1]);
    }

    jQuery(".wpcf7-form input[name='your-name']").attr('value','Nombre');
    jQuery(".wpcf7-form input[name='your-email']").attr('value','Email');
    jQuery(".wpcf7-form input[name='your-subject']").attr('value','Asunto');
    jQuery(".wpcf7-form textarea[name='your-message']").attr('value','Mensaje');
    
    jQuery(".wpcf7-form input[name='your-name']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Nombre') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form input[name='your-name']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Nombre');
    });
    
    jQuery(".wpcf7-form input[name='your-email']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Email') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form input[name='your-email']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Email');
    });
    
    jQuery(".wpcf7-form input[name='your-subject']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Asunto') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form input[name='your-subject']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Asunto');
    });
    
    jQuery(".wpcf7-form textarea[name='your-message']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Mensaje') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form textarea[name='your-message']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Mensaje');
    });
        
    /*jQuery(".post .media").mouseover(function(){
        jQuery(this).find('.watermark').show();
    }).mouseout(function(){
        jQuery(this).find('.watermark').hide();
    });
        
    jQuery(".post .media").click(function(e) {
        e.preventDefault();
        var pid = jQuery(this).parent(".post").attr("id");
        var arr = pid.split("-");
        ajaxCall(arr[1]);
    });*/
    
    jQuery("#post-lightbox-close").click(function() {
        jQuery("#post-lightbox").fadeOut(500);
    });
    
    jQuery("#post-lightbox-close").mouseover(function() {
        jQuery(this).css("opacity","0.8");
    });
    jQuery("#post-lightbox-close").mouseout(function() {
        jQuery(this).css("opacity","1");
    });
    
    jQuery("#post-lightbox-overlay").click(function() {
        jQuery("#post-lightbox").fadeOut(500);
    });
    
    jQuery("#post-lightbox-previous-post").click(function() {
        var pid = jQuery(this).parent().find("#prev-post-link").html();
        ajaxCall(pid);
    });
    
    jQuery("#post-lightbox-previous-post").mouseover(function() {
        jQuery(this).css("opacity","0.8");
    });
    jQuery("#post-lightbox-previous-post").mouseout(function() {
        jQuery(this).css("opacity","1");
    });
    
    jQuery("#post-lightbox-next-post").click(function() {
        var pid = jQuery(this).parent().find("#next-post-link").html();
        ajaxCall(pid);
    });
    
    jQuery("#post-lightbox-next-post").mouseover(function() {
        jQuery(this).css("opacity","0.8");
    });
    jQuery("#post-lightbox-next-post").mouseout(function() {
        jQuery(this).css("opacity","1");
    });
    
    jQuery(window).scroll(function(){
        var top =  jQuery('#post-lightbox-container'); 
        if (jQuery(this).scrollTop() > 250){
            if(top.css('position')!='fixed'){ 
                top.css('position','fixed'); 
            }
        }
        else{
            if(top.css('position')!='static'){
                top.css('position','static');
            }
        }
    });

});


jQuery(document).on("mouseover", ".post .media", function() {
    jQuery(this).find('.watermark').show();
});

jQuery(document).on("mouseout", ".post .media", function() {
    jQuery(this).find('.watermark').hide();
});

jQuery(document).on("click", ".post .media", function() {    
    var pid = jQuery(this).parent(".post").attr("id");
    var arr = pid.split("-");
    ajaxCall(arr[1]);    
});
    
function ajaxCall(pid){
    jQuery.ajax({
            type       : "GET",
            data       : {postId : pid},
            dataType   : "html",
            url        : "http://www.rokbar.com.pe/wp/wp-content/themes/biznetbox/ajaxLoopHandler.php",
            //url        : "http://localhost/rokbar/wp-content/themes/biznetbox/ajaxLoopHandler.php",
            success    : function(data){
                $data = jQuery(data);
                jQuery("#post-lightbox-content").html($data);
                jQuery("#post-lightbox-container").center();
                if (stButtons){stButtons.locateElements();}
                jQuery("#post-lightbox").fadeIn(500);
            },
            error     : function(jqXHR, textStatus, errorThrown) {
                //alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
    });   
}

jQuery.fn.center = function () {
    //this.css("position","absolute");
    
    var width = jQuery(window).width();
    var factor = 0.5;
    if (width <= 480){
        factor = 0.9;
    } else if (width <= 720){
        factor = 0.8;
    }
    
    var elementWidth = factor * width;
    
    this.css("width", elementWidth + "px");
    this.css("margin-left", "-" + (elementWidth/2) + "px");
    
    /*this.css("top", Math.max(0, ((jQuery(window).height() - jQuery(this).outerHeight()) / 2) + 
                                                jQuery(window).scrollTop()) + "px");
    alert("left: " + jQuery(window).width() + ", " + jQuery(this).width());
    this.css("left", Math.max(0, ((jQuery(window).width() - jQuery(this).width()) / 2) + 
                                                jQuery(window).scrollLeft()) + "px");   */                                   
    return this;
}